<?php

namespace app\common\model;

use think\Model;

class District extends Model
{
    // 表名
    protected $name = 'district';
    // 自动写入时间戳字段
//    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
//    protected $createTime = 'createtime';
    // 更新时间字段
//    protected $updateTime = false;
    // 追加属性
    protected $append = [];

    const LEVEL_COUNTRY     = 'country'; // 国家
    const LEVEL_PROVINCE    = 'province'; // 省
    const LEVEL_CITY        = 'city'; // 市
    const LEVEL_DISTRICT    = 'district'; // 区
    const LEVEL_STREET      = 'street'; // 街道

    const TYPE_STORE        = 1; // 分会区域
    const TYPE_SHOP         = 2; // 店铺区域

    public static function getTypeList(){
        return [
            self::TYPE_SHOP     => '店铺区域',
            self::TYPE_STORE    => '分会区域',
        ];
    }
    public static function getTypeText($type){
        $text = '';
        switch($type){
            case self::TYPE_SHOP:
                $text = "店铺区域";
                break;
            case self::TYPE_STORE:
                $text = "分会区域";
                break;
            case self::TYPE_SHOP | self::TYPE_STORE:
                $text = "店铺区域,分会区域";
                break;
        }
        return $text;
    }
    public static function districtAll($type = '')
    {
        $district_id = self::where('parent_id', 0)->value('id');
        $result = self::getDownDistrict($district_id, $type);
        return $result;
    }

    public static function getDownDistrict($parent_id, $type = '')
    {
        $where = [];
        if($type){
            $where = 'type & '. $type;
        }
        $data = self::where('parent_id', $parent_id)->where($where)->field('id, parent_id, name,shop_name')->select();
        foreach ($data as $key => $value) 
        {
            $data[$key]['down'] = self::getDownDistrict($value['id'], $type);
        }
        return $data;
    }

    public static function getArea($parent_id, $type = '')
    {
        $where = [];
        if($type){
            $where = 'type & '. $type;
        }
        return self::where('parent_id', $parent_id)->where($where)->field('id, parent_id, name, lat,lng,shop_name')->select();
    }

    /**
     * 通过上及id获取地区列表
     */
    public static function getAllDistrict($parentId = 0, $fields = ['id', 'name'])
    {
        return model('District')->where('parent_id', $parentId)->field($fields)->order('id ASC')->select();
    }

    /**
     * 删除地区并删除下面的子级地区
     */
    public static function delDistrict($id)
    {
        $count = model('District')->where('id', $id)->delete();
        self::delDistrictEvent($id);
        return $count;
    }

    /**
     * 删除地区事件
     */
    public static function delDistrictEvent($id)
    {
        $list = model('District')->where('parent_id', $id)->select();
        if (!empty($list)) {
            foreach ($list AS $value) {
                self::delDistrict($value['id']);
            }
        }
    }
}
